#include "../ini/crystal.h"
#include "../frame.h"
#include "../io/xyz.h"

#define ERROR prog_result = -1; goto END

int main( int argc, const char *argv[] )
{
	
	int              prog_result = 0;
	mds_frame        frame       = MDS_FRAME_INIT;
	mds_vector_set   position    = NULL;
	size_t           np          = 0;
	mds_vector       box;
	mds_uvector      cr;
	size_t           nc;
	Real             noise;
	
	if( argc <= 5 )
	{
		fprintf( stderr, "%s box_x box_y box_z noise num_particules \n", argv[0]);
		ERROR;
	}
	
	box.x = strtod( argv[1], NULL);
	box.y = strtod( argv[2], NULL);
	box.z = strtod( argv[3], NULL);
	noise = strtod( argv[4], NULL);
	np    = strtol( argv[5], NULL, 10);

	nc    = mds_crystal_close_packed_count( &cr, np, &box);
	fprintf( stderr, "crystal for %u: %u as 4 * [%u %u %u]\n", (unsigned)np, (unsigned) nc, (unsigned) cr.x, (unsigned) cr.y, (unsigned) cr.z);

	if( mds_frame_load( &frame, nc, 1 ) < 0 )
	{
		mds_perror( stderr, "mds_frame_load" );
		ERROR;
	}
	
	position = frame.sets[0];
	np       = frame.num_particles;
	mds_crystal_build_fcc( position, np, &cr, &box, noise);
	(void) mds_xyz_save( "fcc.xyz", position, np, "fcc", 0);
	
	
END:
	mds_frame_quit( &frame );
	return prog_result;
}
